package com.bigfish.gbs.controller;


import com.bigfish.gbs.entity.Merchant;
import com.bigfish.gbs.service.MerchantService;
import com.bigfish.gbs.vo.Reg;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.ValidationEvent;
import java.io.IOException;

//注册跳转控制
@Controller
@RequestMapping("/merchant")
public class MerchantController extends BasicAction {
	@Autowired
	private MerchantService merchantService;

	@RequestMapping(value = "/toregist", method = RequestMethod.GET)
	public String toregist() {

		return "redirect:/regist.jsp";
	}


	@RequestMapping(value = "/regist", method = RequestMethod.POST)
	public String regist(Merchant merchant, HttpServletRequest request) {

		int rows = merchantService.regist(merchant);

		if (rows == 1) {

			request.getSession().setAttribute("merchantname", merchant.getMerchantname());

			return "redirect:/product/frontlist";
		}

		request.setAttribute("msg", "注册失败");

		return "forward:/merchant/toregist";
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String tologin() {

		return "redirect:/login.jsp";
	}


	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(Merchant merchant, Model model, HttpServletRequest request) {

		Merchant exitmerchant = merchantService.login(merchant);

		if (exitmerchant == null) {

			model.addAttribute("msg", "用户名或密码错误");
			return "forward:/login.jsp";
		}

		request.getSession()
				.setAttribute("frontmerchant", exitmerchant.getMerchantname());
		request.getSession()
				.setAttribute("frontmerchantId", exitmerchant.getMid());

		String path = (String) request.getSession().getAttribute("orderpath");

		if (path != null) {

			return "redirect:" + path;

		}
		return "redirect:/product/frontlist";
	}

	@RequestMapping(value = "/checkmerchant", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody
	Reg checkmerchantname(String merchantname) {

		int count = merchantService.checkMerchantname(merchantname);
		Reg reg = new Reg(200, "可以注册");

		if (count > 0) {

			reg.setCode(500);
			reg.setMsg("账号已经存在");

		}

		return reg;
	}


/**
@RequestMapping(value = "/goModify", method = RequestMethod.GET)
public ModelAndView goModify(@RequestParam String id){
	System.out.println(id);
	Merchant merchant = merchantService.findById(id);
	ModelAndView m = new ModelAndView("modify");
	m.addObject("merchant", merchant);
	return m;
}
*/

//商家中心merchant_center.jsp跳转
@RequestMapping(value = "/go_merchant_center", method = RequestMethod.GET)
public String gomodify() {

	return "redirect:/merchant_center.jsp";
}

@RequestMapping(value = "go_merchant_info", method = RequestMethod.GET)
public String gomechantrinfo() {

        return "redirect:/merchant_info.jsp";
    }

}